﻿using ReactiveUI;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;

namespace LS.Screening.UI.Common.Models
{
    public class MenuItem : ViewModel
    {
        /// <summary>
        /// 程序集名称
        /// </summary>
        string _assebmlyName;

        /// <summary>
        /// 视图全称
        /// </summary>
        string _viewName;

        string _name;
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get => _name; set => this.RaiseAndSetIfChanged(ref _name, value); }

        ScrollBarVisibility horizontalScrollBarVisibilityRequirement = ScrollBarVisibility.Auto;
        /// <summary>
        /// 水平滚动条可见性
        /// </summary>
        public ScrollBarVisibility HorizontalScrollBarVisibilityRequirement { get => horizontalScrollBarVisibilityRequirement; set => this.RaiseAndSetIfChanged(ref horizontalScrollBarVisibilityRequirement, value); }

        ScrollBarVisibility verticalScrollBarVisibilityRequirement = ScrollBarVisibility.Auto;
        /// <summary>
        /// 垂直滚动条可见性
        /// </summary>
        public ScrollBarVisibility VerticalScrollBarVisibilityRequirement { get => verticalScrollBarVisibilityRequirement; set => this.RaiseAndSetIfChanged(ref verticalScrollBarVisibilityRequirement, value); }

        Thickness thicknessRequirement = new Thickness(16);
        /// <summary>
        /// 边距
        /// </summary>
        public Thickness MarginRequirement { get => thicknessRequirement; set => this.RaiseAndSetIfChanged(ref thicknessRequirement, value); }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="assemblyName"></param>
        /// <param name="viewName"></param>
        public MenuItem(string name, string assemblyName, string viewName)
        {
            _name = name;
            _assebmlyName = assemblyName;
            _viewName = viewName;
        }

        object? _content;
        /// <summary>
        /// 内容对象
        /// </summary>
        public object? Content => _content ?? CreateContent();

        /// <summary>
        /// 创建对象
        /// </summary>
        /// <returns></returns>
        private object? CreateContent()
        {
            var assembly = Assembly.Load(_assebmlyName);
            var type = assembly.GetType(_viewName);
            var content = Activator.CreateInstance(type);
            return content;
        }
    }
}
